package com.zhiwen.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @Auther: wuzhiwen
 * @Date: 2018/12/27 10:13
 * @Description:
 */
public class NIOClient {
    //定义缓冲区大小
    private static int blockSize = 4096;
    //发送缓冲区
    private static ByteBuffer sendBuffer = ByteBuffer.allocate(blockSize);
    //接收缓冲区
    private static ByteBuffer receiveBuffer = ByteBuffer.allocate(blockSize);
    //设置服务端ip和port
    private final static InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1", 8801);

    public static void main(String[] args) throws IOException {
        //打开管道
        SocketChannel socketChannel = SocketChannel.open();
        //不阻塞
        socketChannel.configureBlocking(false);
        //打开选择器
        Selector selector = Selector.open();
        //注册链接
        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        socketChannel.connect(serverAddress);

        Set<SelectionKey> selectionKeys;
        Iterator<SelectionKey> iterator;
        SelectionKey selectionKey;
        SocketChannel client;
        String sendMessage;
        String resiveMessage;
        int count = 0;
        int flag = 0;
        while (true) {
            selector.select();
            selectionKeys = selector.selectedKeys();
            iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                selectionKey = iterator.next();
                if (selectionKey.isConnectable()) {
                    System.out.println("client connect server");
                    client = (SocketChannel) selectionKey.channel();
                    if (client.isConnectionPending()) {
                        client.finishConnect();
                        System.out.println("客户端完成链接操作");
                        sendBuffer.clear();
                        sendBuffer.put("Hello server".getBytes());
                        sendBuffer.flip();
                        client.write(sendBuffer);
                    }
                    client.register(selector, SelectionKey.OP_READ);
                }
                if (selectionKey.isReadable()) {
                    client = (SocketChannel) selectionKey.channel();
                    receiveBuffer.clear();
                    count = client.read(receiveBuffer);
                    if (count > 0) {
                        resiveMessage = new String(receiveBuffer.array(), 0, count);
                        System.out.println("客户端接收到服务端数据" + resiveMessage);
                        client.register(selector, SelectionKey.OP_WRITE);
                    }
                }
                if (selectionKey.isWritable()) {
                    sendBuffer.clear();
                    client = (SocketChannel) selectionKey.channel();
                    sendMessage = "message to server" + flag++;
                    sendBuffer.put(sendMessage.getBytes());
                    sendBuffer.flip();
                    client.write(sendBuffer);
                    System.out.println("客服端发送数据给服务端" + sendMessage);
                    client.register(selector, SelectionKey.OP_READ);
                }
            }
            selectionKeys.clear();
        }
    }
}
